package com.easyj.base.security.service;

import com.easyj.base.security.SimpleUser;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;


public class TokenUtil {
 
    private final static String prex="U";//前缀

    //token过期时间 second
    public static Integer  TokenExpiration=60*60;  //one hour

    
    private static final String tokenHeader="Authorization";

   private static java.util.Random random=new java.util.Random(28445656);

    public static String generateToken(SimpleUser userDetails) {
        return (prex+userDetails.getId()+"_"+random.nextInt())+(new Date()).getTime();
    }

    public static String generateToken(Long userId) {
        return prex+userId;
    }
    
    public static String getToken(HttpServletRequest request){
        final String requestHeader = request.getHeader(tokenHeader);
        if (requestHeader != null && requestHeader.startsWith("Bearer ")) {
            return requestHeader.substring(7);
        }
        return null;
    }
   

   
}

